<!DOCTYPE html>
<html lang="zh-CN">





<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/apple-touch-icon.png">
  <link rel="icon" type="image/png" href="/blog/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="兴趣使然的个人博客">
  <meta name="author" content="Ilirus">
  <meta name="keywords" content>
  <title>Kotlin学习笔记02 - Ilirus</title>

  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css">


  <link rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css">
  <link rel="stylesheet" href="/blog/lib/hint/hint.min.css">

  
    <link rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/atom-one-light.min.css">
  

  


<!-- 主题依赖的图标库，不要自行修改 -->
<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_yg9cfy8wd6.css">

<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">

<link rel="stylesheet" href="/blog/css/main.css">

<!-- 自定义样式保持在最底部 -->


  <script src="/blog/js/utils.js"></script>
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/blog/">&nbsp;<strong>Ilirus</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="view intro-2" id="background" parallax=true
         style="background: url('/blog/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container text-center white-text fadeInUp">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2018-12-06 15:08">
      2018年12月6日 下午
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.1k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      15
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <div class="post-content mx-auto" id="post">
            
            <article class="markdown-body">
              <h1 id="Kotlin基本语法"><a href="#Kotlin基本语法" class="headerlink" title="Kotlin基本语法"></a>Kotlin基本语法</h1><h2 id="变量、常量的定义"><a href="#变量、常量的定义" class="headerlink" title="变量、常量的定义"></a>变量、常量的定义</h2><p><code>[修饰符] &lt;var|val&gt; &lt;名称&gt; : &lt;类型&gt; = &lt;赋值&gt;</code></p>
<pre><code class="hljs kotlin"><span class="hljs-comment">//var 代表变量，可被更改</span>
<span class="hljs-keyword">var</span> age: <span class="hljs-built_in">Int</span> = <span class="hljs-number">18</span>
<span class="hljs-comment">//val 代表常量, 定义后不能被修改, 定义时必须赋值</span>
<span class="hljs-keyword">val</span> name: String = <span class="hljs-string">"Ilirus"</span></code></pre>
<h2 id="基本数据类型"><a href="#基本数据类型" class="headerlink" title="基本数据类型"></a>基本数据类型</h2><pre><code class="hljs kotlin"><span class="hljs-keyword">var</span> boolean:<span class="hljs-built_in">Boolean</span> = <span class="hljs-literal">false</span>
<span class="hljs-keyword">var</span> char:<span class="hljs-built_in">Char</span> = <span class="hljs-string">'a'</span>
<span class="hljs-keyword">var</span> byte:<span class="hljs-built_in">Byte</span> = <span class="hljs-number">127</span>
<span class="hljs-keyword">var</span> short:<span class="hljs-built_in">Short</span> = <span class="hljs-number">32767</span>
<span class="hljs-keyword">var</span> int:<span class="hljs-built_in">Int</span> = <span class="hljs-number">2147483647</span>
<span class="hljs-keyword">var</span> long:<span class="hljs-built_in">Long</span> = <span class="hljs-number">9223372036854775807</span>
<span class="hljs-keyword">var</span> float:<span class="hljs-built_in">Float</span> = <span class="hljs-number">2.33f</span>
<span class="hljs-keyword">var</span> double:<span class="hljs-built_in">Double</span> = <span class="hljs-number">3.1415926</span></code></pre>
<a id="more"></a>
<h2 id="类型检测与自动转换"><a href="#类型检测与自动转换" class="headerlink" title="类型检测与自动转换"></a>类型检测与自动转换</h2><pre><code class="hljs kotlin"><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">getStrLength</span><span class="hljs-params">(o: <span class="hljs-type">Any</span>)</span></span>: <span class="hljs-built_in">Int</span> &#123;
    <span class="hljs-keyword">if</span>(o <span class="hljs-keyword">is</span> String)&#123;
        <span class="hljs-comment">//进行类型判断后, o会自动被转换为String类型</span>
        <span class="hljs-keyword">return</span> o.length
    &#125;
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
&#125;</code></pre>
<h2 id="返回空值与空值检测"><a href="#返回空值与空值检测" class="headerlink" title="返回空值与空值检测"></a>返回空值与空值检测</h2><p>如果引用或函数的返回值可能为空值，则必须显式标记nullable<br><pre><code class="hljs kotlin"><span class="hljs-comment">//在类型后面加上一个问号, 表示这个对象可能为空</span>
<span class="hljs-comment">//用两个感叹号表示一定不为空</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">getStuName</span><span class="hljs-params">(stu: <span class="hljs-type">Any</span>?)</span></span>: String? &#123;
    <span class="hljs-keyword">if</span>(stu!=<span class="hljs-literal">null</span>)&#123;
        <span class="hljs-keyword">return</span> stu.getName
    &#125;
    <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>
&#125;</code></pre></p>
<p>Kotlin的空值检测能有效的避免<code>NPE</code>(NullPointException) 的出现</p>
<p>例如这句代码<code>student?.getName()</code>，只会在student不为空时执行<br>相反，这句代码<code>student?:student=Student()</code>，只会在student为空时执行</p>
<p>如果要执行代码块的话，可以这样写<br><pre><code class="hljs kotlin"><span class="hljs-comment">//data不为空执行</span>
<span class="hljs-keyword">data</span>?.let&#123;
    <span class="hljs-comment">// do something</span>
&#125;

<span class="hljs-comment">//data为空执行</span>
<span class="hljs-keyword">data</span>?:let&#123;
    <span class="hljs-comment">// do something</span>
&#125;

<span class="hljs-comment">//或者</span>

<span class="hljs-comment">//data不为空执行</span>
<span class="hljs-keyword">data</span>?.run&#123;
    <span class="hljs-comment">// do something</span>
&#125;

<span class="hljs-comment">//data为空执行</span>
<span class="hljs-keyword">data</span>?:run&#123;
    <span class="hljs-comment">// do something</span>
&#125;</code></pre></p>
<h2 id="run与let"><a href="#run与let" class="headerlink" title="run与let"></a>run与let</h2><pre><code class="hljs kotlin"><span class="hljs-keyword">val</span> str:String = <span class="hljs-string">"Ilirus"</span>

str?.let&#123;
    println(it)
&#125;
str?.let&#123; s -&gt;
    println(s)
&#125;
str?.run&#123;
    println(<span class="hljs-keyword">this</span>)
&#125;</code></pre>
<h2 id="when关键词"><a href="#when关键词" class="headerlink" title="when关键词"></a><code>when</code>关键词</h2><p>when类似于Java中的switch<br><pre><code class="hljs kotlin"><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">printType</span><span class="hljs-params">(o:<span class="hljs-type">Any</span>)</span></span> &#123;
    <span class="hljs-keyword">when</span>(o)&#123;
        <span class="hljs-keyword">is</span> <span class="hljs-built_in">Int</span> -&gt; &#123;
            println(<span class="hljs-string">"Int"</span>)
            println(o)
        &#125;
        <span class="hljs-keyword">is</span> String -&gt; &#123;
            println(<span class="hljs-string">"String"</span>)
            println(o.toUpperCase())
        &#125;
        <span class="hljs-keyword">is</span> <span class="hljs-built_in">Char</span> -&gt; println(<span class="hljs-string">"Char"</span>)
        <span class="hljs-keyword">else</span> -&gt; println(<span class="hljs-string">"Other"</span>)
    &#125;
&#125;</code></pre></p>
<h2 id="in关键词"><a href="#in关键词" class="headerlink" title="in关键词"></a><code>in</code>关键词</h2><p>判断数字是否在某一区间内<br><pre><code class="hljs kotlin"><span class="hljs-keyword">if</span>( x <span class="hljs-keyword">in</span> <span class="hljs-number">1</span>..<span class="hljs-number">100</span>)&#123;
    println(<span class="hljs-string">"yes"</span>)
&#125;

<span class="hljs-keyword">if</span>( x !<span class="hljs-keyword">in</span> <span class="hljs-number">1</span>..<span class="hljs-number">100</span>)&#123;
    println(<span class="hljs-string">"no"</span>)
&#125;</code></pre></p>
<p>遍历集合<br><pre><code class="hljs kotlin"><span class="hljs-keyword">for</span>(item <span class="hljs-keyword">in</span> arr)&#123;
    println(<span class="hljs-string">"item: <span class="hljs-variable">$item</span>"</span>)
&#125;

<span class="hljs-keyword">for</span>((index,item) <span class="hljs-keyword">in</span> arr.withIndex())&#123;
    println(<span class="hljs-string">"<span class="hljs-variable">$index</span> - <span class="hljs-variable">$item</span>"</span>)
&#125;

<span class="hljs-comment">//判断集合中是否含有指定对象</span>
<span class="hljs-keyword">if</span>(student <span class="hljs-keyword">in</span> person)&#123;
    println(<span class="hljs-string">"yes"</span>)
&#125;</code></pre></p>
<h2 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h2><h3 id="函数的声明"><a href="#函数的声明" class="headerlink" title="函数的声明"></a>函数的声明</h3><pre><code class="hljs kotlin"><span class="hljs-comment">//[修饰符] fun &lt;函数名&gt;([参数]) : &lt;返回类型&gt;</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">sum</span><span class="hljs-params">(a: <span class="hljs-type">Int</span>, b: <span class="hljs-type">Int</span>)</span></span>: <span class="hljs-built_in">Int</span> &#123;
    <span class="hljs-keyword">return</span> a+b
&#125;

<span class="hljs-comment">//方法体只有一条语句时可简化</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">sum</span><span class="hljs-params">(a: <span class="hljs-type">Int</span>, b: <span class="hljs-type">Int</span>)</span></span> = a+b

<span class="hljs-comment">//方法为public时, 必须显式声明返回类型</span>
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">sum</span><span class="hljs-params">(a: <span class="hljs-type">Int</span>, b: <span class="hljs-type">Int</span>)</span></span>: <span class="hljs-built_in">Int</span> = a+b</code></pre>
<p>不返回值<br><pre><code class="hljs kotlin"><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">printSum</span><span class="hljs-params">(a: <span class="hljs-type">Int</span>, b: <span class="hljs-type">Int</span>)</span></span>: <span class="hljs-built_in">Unit</span> &#123;
    println(a+b)
&#125;

<span class="hljs-comment">//可省略Unit关键词, 即使方法是public修饰的</span>
<span class="hljs-keyword">public</span> printSum(a: <span class="hljs-built_in">Int</span>, b: <span class="hljs-built_in">Int</span>) &#123;
    println(a+b)
&#125;</code></pre></p>
<h3 id="函数默认参数"><a href="#函数默认参数" class="headerlink" title="函数默认参数"></a>函数默认参数</h3><pre><code class="hljs kotlin"><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">hello</span><span class="hljs-params">(name: <span class="hljs-type">String</span> = <span class="hljs-string">"User"</span>)</span></span>: String = <span class="hljs-string">"Hello <span class="hljs-variable">$name</span>"</span></code></pre>
<p>直接调用<code>hello()</code>，将得到默认字符串<code>&quot;Hello User&quot;</code><br>也可以自己传入参数<code>hello(&quot;Ilirus&quot;)</code>，来得到<code>&quot;Hello Ilirus&quot;</code></p>
<p>如果函数有较多的参数时，可以使用多行参数的写法<br><pre><code class="hljs kotlin"><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">hello</span><span class="hljs-params">(name: <span class="hljs-type">String</span> = <span class="hljs-string">"User"</span>,
          sid: <span class="hljs-type">Int</span> = <span class="hljs-number">0</span>)</span></span>: String &#123;
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello <span class="hljs-variable">$name</span> Your SID is <span class="hljs-variable">$sid</span>"</span>
&#125;</code></pre></p>
<h3 id="变参函数"><a href="#变参函数" class="headerlink" title="变参函数"></a>变参函数</h3><p>使用<code>vararg</code>来表示这是一个变长参数<br><pre><code class="hljs kotlin"><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">hasEmpty</span><span class="hljs-params">(<span class="hljs-keyword">vararg</span> array: <span class="hljs-type">Any</span>?)</span></span>: <span class="hljs-built_in">Boolean</span> &#123;
    <span class="hljs-keyword">return</span> array.any&#123; it == <span class="hljs-literal">null</span> || <span class="hljs-string">""</span>.equals(it) &#125;
&#125;</code></pre></p>
<h3 id="扩展函数"><a href="#扩展函数" class="headerlink" title="扩展函数"></a>扩展函数</h3><p>给目标类添加一个方法，使其可以在当前类中被使用</p>
<p>receiverType: 表示函数的接收者，也就是函数扩展的对象<br>functionName: 扩展函数的名称<br>params: 扩展函数的参数，可以为NULL</p>
<pre><code class="hljs kotlin"><span class="hljs-function"><span class="hljs-keyword">fun</span> receiverType.<span class="hljs-title">functionName</span><span class="hljs-params">(params)</span></span>&#123;
    <span class="hljs-comment">//body</span>
&#125;</code></pre>
<p>例如<br><pre><code class="hljs kotlin"><span class="hljs-comment">//定义一个User类</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span></span>(
    <span class="hljs-keyword">var</span> name: String? = <span class="hljs-literal">null</span>
)

<span class="hljs-comment">//在另一个类(当前类)中扩展User类</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> User.<span class="hljs-title">setName</span><span class="hljs-params">(name: <span class="hljs-type">String</span>)</span></span>&#123;
    <span class="hljs-keyword">this</span>.name = name
&#125;

<span class="hljs-function"><span class="hljs-keyword">fun</span> User.<span class="hljs-title">getName</span><span class="hljs-params">()</span></span>: String? &#123;
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.name
&#125;

<span class="hljs-comment">//在当前类中执行User类的扩展方法</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">main</span><span class="hljs-params">(arg:<span class="hljs-type">Array</span>&lt;<span class="hljs-type">String</span>&gt;)</span></span>&#123;
    <span class="hljs-keyword">var</span> usr = User()
    println(usr.setName(<span class="hljs-string">"Ilirus"</span>))
    println(usr.getName())
&#125;</code></pre></p>
<h4 id="注意事项"><a href="#注意事项" class="headerlink" title="注意事项"></a>注意事项</h4><p><strong>其实扩展函数是静态解析的，而并不是真的给类添加了方法</strong><br><pre><code class="hljs kotlin"><span class="hljs-keyword">open</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span></span>&#123;
    <span class="hljs-comment">//</span>
&#125;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>:<span class="hljs-type">Animal</span></span>()

<span class="hljs-keyword">object</span> Main &#123;
    <span class="hljs-function"><span class="hljs-keyword">fun</span> Animal.<span class="hljs-title">bark</span><span class="hljs-params">()</span></span> = <span class="hljs-string">"animal"</span>

    <span class="hljs-function"><span class="hljs-keyword">fun</span> Dog.<span class="hljs-title">bark</span><span class="hljs-params">()</span></span> = <span class="hljs-string">"dog"</span>

    <span class="hljs-function"><span class="hljs-keyword">fun</span> Animal.<span class="hljs-title">printBark</span><span class="hljs-params">(anim: <span class="hljs-type">Animal</span>)</span></span>&#123;
        println(anim.bark())
    &#125;

    <span class="hljs-meta">@JvmStatic</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">main</span><span class="hljs-params">(args: <span class="hljs-type">Array</span>&lt;<span class="hljs-type">String</span>&gt;)</span></span> &#123;
        Animal().printBark(Dog())
    &#125;
&#125;</code></pre></p>
<p>最终输出的是<code>animal</code>，而不是<code>dog</code></p>
<p>因为扩展方法是静态解析的，在添加扩展方法的时候类型为<code>Animal</code>，那么即便运行时传入了子类对象，也依旧会执行参数中声明时类型的方法。 </p>
<h3 id="函数入参"><a href="#函数入参" class="headerlink" title="函数入参"></a>函数入参</h3><p>在Kotlin中，可以将一个函数作为参数传递给另一个函数<br><pre><code class="hljs kotlin"><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">checkEach</span><span class="hljs-params">(array: <span class="hljs-type">Array</span>&lt;<span class="hljs-type">Any</span>&gt;, callback: (<span class="hljs-type">Any</span>) -&gt; <span class="hljs-type">Unit</span>)</span></span>: <span class="hljs-built_in">Unit</span> &#123;
    <span class="hljs-keyword">for</span>(arr <span class="hljs-keyword">in</span> array)&#123;
        callback(arr)
    &#125;
&#125;

<span class="hljs-keyword">var</span> method:(Any)-&gt;<span class="hljs-built_in">Unit</span> = &#123;
    println(it)
&#125;

checkEach(array, method)</code></pre></p>

            </article>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/blog/categories/学习笔记/">学习笔记</a>
                    
                      <a class="hover-with-bg" href="/blog/categories/学习笔记/Kotlin/">Kotlin</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/blog/tags/Kotlin/">Kotlin</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a href="https://zh.wikipedia.org/wiki/Wikipedia:CC_BY-SA_3.0%E5%8D%8F%E8%AE%AE%E6%96%87%E6%9C%AC" rel="nofollow noopener">CC BY-SA 3.0协议</a> 。转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <div class="post-prev col-6">
                    
                    
                      <a href="/blog/2018/12/08/Kotlin学习笔记03/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Kotlin学习笔记03</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </div>
                  <div class="post-next col-6">
                    
                    
                      <a href="/blog/2018/12/05/Kotlin学习笔记01/">
                        <span class="hidden-mobile">Kotlin学习笔记01</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </div>
                </div>
              
            </div>

            
              <!-- Comments -->
              <div class="comments" id="comments">
                
                
  <div id="lv-container" data-id="city" data-uid="MTAyMC80NTMxNi8yMTgyOQ==">
    <script type="text/javascript">
      function loadLivere() {
        addScript('https://cdn-city.livere.com/js/embed.dist.js');
      }
      createObserver(loadLivere, 'lv-container');
    </script>
    <noscript>为正常使用来必力评论功能请允许 JavaScript 运行</noscript>
  </div>


              </div>
            
          </div>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
    <div>
      <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a>
      <i class="iconfont icon-love"></i>
      <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener">
        <span>Fluid</span></a>
    </div>
    

    

    
  </div>
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/debouncer.js" ></script>
<script  src="/blog/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/blog/js/lazyload.js" ></script>
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/blog/js/clipboard-use.js" ></script>







  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: 'article.markdown-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "Kotlin学习笔记02&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/blog/js/local-search.js" ></script>
  <script>
    var path = "/blog/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script type="text/javascript">
      //定义获取词语下标
      var a_idx = 0;
      jQuery(document).ready(function ($) {
        //点击body时触发事件
        $("body").click(function (e) {
          //需要显示的词语
          var a = new Array("富强", "民主", "文明", "和谐", "自由", "平等", "公正", "法治", "爱国", "敬业", "诚信", "友善");
          //设置词语给span标签
          var $i = $("<span/>").text(a[a_idx]);
          //下标等于原来下标+1  余 词语总数
          a_idx = (a_idx + 1) % a.length;
          //获取鼠标指针的位置，分别相对于文档的左和右边缘。
          //获取x和y的指针坐标
          var x = e.pageX, y = e.pageY;
          //在鼠标的指针的位置给$i定义的span标签添加css样式
          $i.css({
            "z-index": 999,
            "top": y - 20,
            "left": x,
            "position": "absolute",
            "font-weight": "bold",
            "color": rand_color()
          });
          // 随机颜色
          function rand_color() {
            return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
          }
          //在body添加这个标签
          $("body").append($i);
          //animate() 方法执行 CSS 属性集的自定义动画。
          //该方法通过CSS样式将元素从一个状态改变为另一个状态。CSS属性值是逐渐改变的，这样就可以创建动画效果。
          //详情请看http://www.w3school.com.cn/jquery/effect_animate.asp
          $i.animate({
            //将原来的位置向上移动180
            "top": y - 180,
            "opacity": 0
            //1500动画的速度
          }, 1500, function () {
            //时间到了自动删除
            $i.remove();
          });
        });
      })
      ;
    </script>
  














<script src="/blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/blog/live2dw/assets/tororo.model.json"},"display":{"position":"left","width":300,"height":300},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
